from pydantic import BaseModel, Field
from datetime import datetime
from typing import Optional

class FaceRecognitionEvent(BaseModel):
    """人脸识别事件模型"""
    door_id: str = Field(..., description="门禁ID")
    time: datetime = Field(default_factory=datetime.now, description="识别时间")
    person_id: str = Field(..., description="人员ID")
    name: str = Field(..., description="人员姓名")
    confidence: float = Field(..., ge=0, le=1, description="识别置信度")
    capture_image: Optional[str] = Field(None, description="抓拍图片base64数据")

class CardSwipeEvent(BaseModel):
    """刷卡事件模型"""
    door_id: str = Field(..., description="门禁ID")
    time: datetime = Field(default_factory=datetime.now, description="刷卡时间")
    card_no: str = Field(..., description="卡号")
    holder_id: str = Field(..., description="持卡人ID")
    holder_name: str = Field(..., description="持卡人姓名")
    event_type: str = Field(..., description="事件类型")

class DoorStateEvent(BaseModel):
    """门禁状态事件模型"""
    door_id: str = Field(..., description="门禁ID")
    state: str = Field(..., description="门禁状态", pattern="^(open|closed)$")
    time: datetime = Field(default_factory=datetime.now, description="状态变更时间")

class AlarmEvent(BaseModel):
    """报警事件模型"""
    door_id: str = Field(..., description="门禁ID")
    alarm_type: str = Field(..., description="报警类型", pattern="^(timeout|force_open)$")
    description: Optional[str] = Field(None, description="报警描述") 